package com.example.administrator.intentbroadcast;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class IntentActivity extends AppCompatActivity {
    Button button = null;
    Button dialer = null;
    Button dialer2 = null;
    EditText phoneNum = null;
    Button broadcaset = null;
    Button broadcast2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent);

        button = (Button)findViewById(R.id.Button01);
        dialer = (Button)findViewById(R.id.Button02);
        dialer2 = (Button)findViewById(R.id.Button03);
        phoneNum = (EditText)findViewById(R.id.phoneNum);
        broadcaset = (Button)findViewById(R.id.broadcast);
        broadcast2 = (Button)findViewById(R.id.broadcast02);

        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });

        dialer.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel://114"));
                startActivity(intent);
            }
        });

        dialer2.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                String call = phoneNum.getText().toString();
                if(PhoneNumberUtils.isGlobalPhoneNumber(call)){
                    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel://"+call));
                    startActivity(intent);
                }
                else{
                    Toast.makeText(IntentActivity.this, R.string.notify_incorrect_phonenum, Toast.LENGTH_LONG).show();
                }
            }
        });

        broadcaset.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IntentActivity.this,Intent_and_BroadCast.class);
                startActivity(intent);
            }
        });

        broadcast2.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IntentActivity.this,IntentReceiverInJavaActivity.class);
                startActivity(intent);
            }
        });

    }
}
